unit MapDelete;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Menus;

type
  TFormMapDelete = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    ListMaps: TListBox;
    Label2: TLabel;
    Label1: TLabel;
    BtnAll: TBitBtn;
    Label3: TLabel;
    Bevel1: TBevel;
    Panel1: TPanel;
    RBDelete: TRadioButton;
    RBClear: TRadioButton;
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnAllClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    diddelete: boolean;
  end;

var
  FormMapDelete: TFormMapDelete;


implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormMapDelete.FormCreate(Sender: TObject);
begin
  diddelete:=false;
end;

procedure TFormMapDelete.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormMapDelete.BtnAllClick(Sender: TObject);
var i: integer;
begin
  for i:=0 to ListMaps.items.count-1 do ListMaps.selected[i]:=true;
end;

procedure TFormMapDelete.BtnOKClick(Sender: TObject);
var
  selected: TStringList;
  i: integer;
  table: TClientDataset;
  didfind: boolean;

   procedure DeleteRelation(dataset: TCustomClientDataset);
   begin
     table.active:=false;
     table.CloneCursor(dataset, false);
     table.filter :='(Map = '''+selected[i]+''')';
     table.filtered := true;
     table.active:=true;
     table.first;
     while not table.EOF do
       table.delete;
   end;

begin
  selected:=TStringList.create;
  for i:=0 to ListMaps.items.count-1 do begin
    if ListMaps.selected[i] then selected.add(ListMaps.items[i]);
  end;
  if selected.count < 1 then begin
    messagedlg('No map levels are selected',mtinformation,[mbok],0);
    exit;
  end;
  diddelete:=true;
  table:=TClientDataset.create(self);
  with table do begin
    for i:=0 to selected.count-1 do begin
      active:=false;
      cloneCursor(Datawell.TableMaps, false);
      filter := '';
      active:=true;
      first;
      didfind:=false;
      while not EOF do begin
        if fieldbyname('Map').asstring = selected[i] then begin
          didfind:=true;
          break;
        end;
        next;
      end;
      if didfind then begin
        //Delete Map
        if RBDelete.checked then delete;
        //Delete Rooms
        DeleteRelation(datawell.TableRooms);
        DeleteRelation(datawell.TableExits);
      end;
    end;
  end;
  table.free;
  selected.free;
  close;
end;

end.
